public class Solution7 {
    //279. 完全平方数
    public int numSquares(int n) {

        int[] square = new int[n];
        int count = 0;
        for (int i = 1; i * i <= n; i++) {
            square[count++] = i * i;
        }

        int[][] dp = new int[count+1][n+1];  //从前i个选，平方和恰好为j的最少数量

        int INF = 0x3f3f3f3f;
        for (int j = 1; j < n+1; j++) {
            dp[0][j] = INF;
        }
        for (int i = 1; i < count+1; i++) {
            for (int j = 1; j < n+1; j++) {
                dp[i][j] = dp[i-1][j];
                if (j >= square[i-1]) {
                    dp[i][j] = Math.min(dp[i][j],dp[i][j-square[i-1]]+1);
                }
            }
        }
        return dp[count][n];

    }
}
